package com.chinaoct.siku.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class RegHtmlUtil {

    /**
     * 用正则获取网上的信息
     *
     * @param mode "<span>\\s*\\n*
     *            <hr>
     *            \\s*\\n*\\t*<br>
     *            ([^/]+)<br>
     *            \\s*\\n*\\t*</div>"
     * @return
     */
    public static String regStr(String sHtml, String mode) {
        Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
        Matcher m = p.matcher(sHtml);
        String reg_s = null;
        if (m.find()) {
            reg_s = m.group(1);
        }
        return reg_s;
    }

    public static String regStrMini(String sHtml, String mode) {
        Pattern p = Pattern.compile(mode);
        Matcher m = p.matcher(sHtml);
        String reg_s = null;
        if (m.find()) {
            reg_s = m.group(1);
        }
        return reg_s;
    }

    /**
     * @param sHtml
     * @param mode 正则
     * @param i 匹配第几个组
     * @return
     */
    public static ArrayList<String> retrieveLinks(String sHtml, String mode, int i) {
        Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
        try {
            if ((sHtml == null) || (sHtml == "")) return null;
            Matcher m = p.matcher(sHtml);
            ArrayList<String> linkList = new ArrayList<String>();
            while (m.find()) {
                String link = m.group(i).trim();
                linkList.add(link);
            }
            return linkList;
        } catch (RuntimeException e) {
            log.info("正则匹配出现异常!");
            e.printStackTrace();
        }
        return null;
    }

    public static ArrayList<List<String>> retrieveLinkss(String sHtml, String mode) {
        Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL + Pattern.MULTILINE);
        try {
            if ((sHtml == null) || (sHtml == "")) return null;
            Matcher m = p.matcher(sHtml);
            ArrayList<List<String>> linkList = new ArrayList<List<String>>();
            int k = 0;
            while (m.find()) {
                List<String> rows = new ArrayList<String>();
                for (int i = 0; i <= m.groupCount(); i++) {
                    String link = m.group(i);
                    link = link == null ? "" : link.trim();
                    rows.add(link);
                }
                k++;
                linkList.add(rows);
            }
            return linkList;
        } catch (RuntimeException e) {
            log.info("正则匹配出现异常!");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param sHtml
     * @param mode 正则
     * @param i 匹配第几个组
     * @return
     */
    public static ArrayList<String> retrieveLinks(String sHtml, String mode, int i, Integer pattern) {
        Pattern p = Pattern.compile(mode, pattern);
        try {
            if ((sHtml == null) || (sHtml == "")) return null;
            Matcher m = p.matcher(sHtml);
            ArrayList<String> linkList = new ArrayList<String>();
            while (m.find()) {
                String link = m.group(i).trim();
                linkList.add(link);
            }
            return linkList;
        } catch (RuntimeException e) {
            log.info("正则匹配出现异常!");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用正则获取网上的信息
     *
     * @param sHtml
     * @param mode "<span>\\s*\\n*
     *            <hr>
     *            \\s*\\n*\\t*<br>
     *            ([^/]+)<br>
     *            \\s*\\n*\\t*</div>"
     * @param i 匹配第几个组
     * @return
     */
    public static String regStr(String sHtml, String mode, int i) {
        Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
        Matcher m = p.matcher(sHtml);
        String reg_s = null;
        if (m.find()) {
            reg_s = m.group(i);
        }
        return reg_s;
    }

    /**
     * 用正则获取网上的信息
     *
     * @param sHtml
     * @param mode "<span>\\s*\\n*
     *            <hr>
     *            \\s*\\n*\\t*<br>
     *            ([^/]+)<br>
     *            \\s*\\n*\\t*</div>"
     * @return
     */
    public static List<String> regStrs(String sHtml, String mode) {
        Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL + Pattern.MULTILINE);
        List<String> list = new ArrayList<String>();
        Matcher m = p.matcher(sHtml);
        if (m.find()) {
            for (int i = 0; i <= m.groupCount(); i++) {
                list.add(m.group(i));
            }
        }
        return list;
    }

    /**
     * 用正则获取网上的信息
     *
     * @param sHtml
     * @param mode "<span>\\s*\\n*
     *            <hr>
     *            \\s*\\n*\\t*<br>
     *            ([^/]+)<br>
     *            \\s*\\n*\\t*</div>"
     * @param i 匹配几个组
     * @return
     */
    public static List<String> regStrBeforeIndex(String sHtml, String mode, int i) {
        Pattern p = Pattern.compile(mode, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
        Matcher m = p.matcher(sHtml);
        List<String> reg_s = new ArrayList<String>();
        if (m.find()) {
            for (int j = 0; j <= i; j++) {
                String str = StringUtils.trimWhitespace(m.group(j));
                reg_s.add(str);
            }
        }
        return reg_s;
    }

    /**
     * 用正则获取网上的信息
     *
     * @param sHtml
     * @param mode "<span>\\s*\\n*
     *            <hr>
     *            \\s*\\n*\\t*<br>
     *            ([^/]+)<br>
     *            \\s*\\n*\\t*</div>"
     * @param i 匹配第几个组
     * @return
     */
    public static String regStr(String sHtml, String mode, int i, int pattern) {
        Pattern p = Pattern.compile(mode, pattern);
        Matcher m = p.matcher(sHtml);
        String reg_s = null;
        if (m.find()) {
            reg_s = m.group(i);
        }
        return reg_s;
    }

    /**
     * 验证字符串是否符合某个正则式
     *
     * @param str
     * @param regex
     * @return
     */
    public static boolean regMathcher(String str, String regex) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * 验证一组表达式里，是否有符合的字符串
     *
     * @param shtml
     * @param modes
     * @return
     */
    public static String regStr(String shtml, List<String> modes) {
        if (modes == null || modes.isEmpty()) return null;

        for (String s : modes) {
            String result = regStr(shtml, s, 0, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
            if (!StringUtils.isEmpty(result)) {
                return result;
            }
        }
        return null;
    }

    //	public static void main(String[] arg) throws IOException {
    //		String html = new String(IOUtils.toByteArray(new FileInputStream(new File("E:\\mytemp\\xiechen2.html"))),"utf-8");
    //
    //		String c = RegHtmlUtil.regStr(html, "ajaxRequest\\(\"(http://.*)\"\\);.*</script>.*<input type=\"hidden\" id=\"CorrelationId\"");
    //		System.out.println(c);
    //	}
}
